package lu.tf.design.factory.unoptimized;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import lu.tf.design.factory.source.card.IQiYiCardService;
import lu.tf.design.factory.source.coupon.CouponResult;
import lu.tf.design.factory.source.coupon.CouponService;
import lu.tf.design.factory.source.goods.DeliverReq;
import lu.tf.design.factory.source.goods.GoodsService;

/**
 * ClassName: PrizeController
 * date: 8/18 21:06
 * Description:
 *
 * @author lu
 */
@Slf4j
public class PrizeController {

    public AwardRes awardToUser(AwardReq req) {
        final String reqJson = JSON.toJSONString(req);
        AwardRes awardRes = null;
        try {
            log.info("奖品开始发放{}, req: {}", req.getuId(), reqJson);
            // 按照不同类型方法商品[1优惠券 2实物商品 3第三方兑换卡(爱奇艺)]
            // 优惠券
            if (req.getAwardType() == 1) {
                final CouponService couponService = new CouponService();
                final CouponResult couponResult = couponService.sendCoupon(req.getuId(), req.getAwardNumber(), req.getBizId());
                if ("0000".equals(couponResult.getCode())) {
                    awardRes = new AwardRes("0000", "发放成功");
                } else {
                    awardRes = new AwardRes("0001", couponResult.getInfo());
                }
            }
            // 实物商品
            else if (req.getAwardType() == 2) {
                final GoodsService goodsService = new GoodsService();
                final DeliverReq deliverReq = new DeliverReq();
                deliverReq.setUserName("hua");
                deliverReq.setUserPhone("183");
                deliverReq.setSku(req.getAwardNumber());
                deliverReq.setOrderId(req.getBizId());
                deliverReq.setConsigneeUserName(req.getExtMap().get("consigneeUserName"));
                deliverReq.setConsigneeUserPhone(req.getExtMap().get("consigneeUserPhone"));
                deliverReq.setConsigneeUserAddress(req.getExtMap().get("consigneeUserAddress"));

                final Boolean isSuccess = goodsService.deliverGoods(deliverReq);
                if (isSuccess) {
                    awardRes = new AwardRes("0000", "发放成功");
                } else {
                    awardRes = new AwardRes("0001", "发放失败");
                }
            }
            // 第三方兑换卡
            else if (req.getAwardType() == 3) {
                final IQiYiCardService iQiYiCardService = new IQiYiCardService();
                iQiYiCardService.grantToken("183", req.getAwardNumber());
                awardRes = new AwardRes("0000", "发放成功");
            }
        } catch (Exception e) {
            log.error("奖品发放失败{}, req: {}", req.getuId(), reqJson, e);
            awardRes = new AwardRes("0001", e.getMessage());
        }
        return awardRes;
    }
}
